package com.thesis.client;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.thesis.object.Dish;
import com.thesis.object.ListDishes;
import com.thesis.object.ListUsers;
import com.thesis.utils.Defines;

public class FoodDetailActivity extends Activity {
	
	private TextView tv_foodname;
	private TextView tv_foodmaterial;
	private TextView tv_foodcooking;
	private TextView tv_foodspeciality;
	private TextView tv_foodunitcost;
	private ImageView iv_foodimage;
	Dish dish_result;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_food_detail);
		
		
		
		ActionBar ab = getActionBar();
		//ab.setTitle(getResources().getString(R.string.title_activity_statistic));
		ab.setDisplayHomeAsUpEnabled(true);
		
		tv_foodname = (TextView) findViewById(R.id.fd_tv_foodname);
		tv_foodmaterial = (TextView) findViewById(R.id.fd_tv_foodmaterial);
		tv_foodcooking = (TextView) findViewById(R.id.fd_tv_foodcooking);
		tv_foodspeciality = (TextView) findViewById(R.id.fd_tv_foodspeciality);
		tv_foodunitcost = (TextView) findViewById(R.id.fd_tv_foodunitcost);
		iv_foodimage = (ImageView) findViewById(R.id.fd_iv_foodimage);
		
		new getDishbyId().execute();
		
		
		
	}

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.food_detail, menu);
//		return true;
//	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	class getDishbyId extends AsyncTask<String, String, String>{

		@Override
		protected String doInBackground(String... params) {
			int a = getIntent().getIntExtra(Defines.ID_OF_FOODDETAIL, -1);
			
			
			for(int i = 0 ; i< ListDishes.getInstance().getListDishes().size();i++){
				if (a == ListDishes.getInstance().getListDishes().get(i).getId()){
					dish_result = ListDishes.getInstance().getListDishes().get(i);
					break;		
				}
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			tv_foodname.setText(dish_result.getName());
			tv_foodmaterial.setText(dish_result.getMaterial());
			tv_foodcooking.setText(dish_result.getCooking());
			tv_foodspeciality.setText(dish_result.getSpeciality());
			tv_foodunitcost.setText(dish_result.getCost()+dish_result.getUnit());
			String imagePath;
			if (!dish_result.getImage().equals("")){
				imagePath = Environment.getExternalStorageDirectory().toString() 
						+ "/restautant/dish/" +dish_result.getImage();
				iv_foodimage.setImageDrawable(Drawable.createFromPath(imagePath));
			} else {
				iv_foodimage.setImageResource(R.drawable.no_image);
			}
			super.onPostExecute(result);
		}
		
	}

}
